OpenRoads Designer CONNECT Edition SDK Help

List all templates in Template Library

The below code iterates all the template definitions from the template library recursively on each category and prints the template names.


//Required References
using System;
using Bentley.CifNET.GeometryModel.SDK;
using System.Diagnostics;

 public void ListAllTemplatesInTemplateLibrary()
        {
            try
            {
                //Get template library path
                string strTemplateLibraryPath = Bentley.CifNET.GeometryModel.SDK.TemplateLibrary.GetDefaultTemplateLibraryPath();

                //Get template library
                Bentley.CifNET.GeometryModel.SDK.TemplateLibrary templateLibary = Bentley.CifNET.GeometryModel.SDK.TemplateLibrary.Load(strTemplateLibraryPath);

                //get root category from template library
                Category rootCategory = templateLibary.RootCategory;

                //Iterate to find templates
                RecursivelyFindTemplates(rootCategory);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        private void RecursivelyFindTemplates(Bentley.CifNET.GeometryModel.SDK.Category parentCategory)
        {
            foreach (var category in parentCategory.Categories)
            {
                //Recursively iterate categories for finding templates
                RecursivelyFindTemplates(category);
            }

            //Iterate each template definition from current catergory
            foreach (var template in parentCategory.Templates)
            {
                Trace.WriteLine(template.Name);          
            }
        }

Output